package com.oa.controller;

import com.oa.vo.Message;
import com.oa.vo.Page;
import com.oa.vo.Result;
import com.oa.pojo.Clz;
import com.oa.service.ClzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@CrossOrigin
@ResponseBody
@Controller
public class ClzController {

    @Autowired
    private ClzService clzService;

    @RequestMapping(value = "/getClzPage")
    public Result getClzPage(Integer pageNo, Integer pageSize){
        if(pageNo == null){
            pageNo=1;
        }
        if(pageSize == null){
            pageSize=3;
        }
        Page page = clzService.getClzPage(pageNo,pageSize);
        Message message = new Message();
        Result result = new Result();
        result.setData(page);
        result.setMessage(message);
        return result;
    }

    @RequestMapping(value = "/getClzList")
    public Result getClzList(){
        List<Clz> clzList = clzService.getClzList();
        Message message = new Message();
        Result result = new Result();
        result.setData(clzList);
        result.setMessage(message);
        return result;
    }

    @RequestMapping(value = "/getClz")
    public Result getClz(Integer classId){
        Clz clz = clzService.getClz(classId);
        Message message = new Message();
        Result result = new Result();
        result.setData(clz);
        result.setMessage(message);
        return result;
    }

    @RequestMapping(value = "/addClz")
    public Result addClz(@RequestBody Clz clz){
        Date date = new Date();
        clz.setCreateTime(date);
        System.out.println(clz);
        clzService.addClz(clz);
        Result result = new Result();
        return result;
    }

    @RequestMapping(value = "/delClz")
    public Result delClz(Integer classId){
        Message message = new Message();
        Result result = new Result();
        if(classId == null){
            message.setCode(200);
            message.setMsg("No ID");
            result.setMessage(message);
            return result;
        }
        message.setCode(100);
        message.setMsg("ok");
        clzService.delClz(classId);
        result.setMessage(message);
        return result;
    }

    @RequestMapping(value = "/updateClz")
    public Result updateClz(@RequestBody Clz clz){
        clzService.updateClz(clz);
        Result result = new Result();
        return result;
    }

}
